package com.example.administrator.mobliesafe;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectContactNumberActivity extends Activity {
    private ListView lv_contact_number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_contact_number);
        lv_contact_number = (ListView) findViewById(R.id.lv_contact_number);
        final List<Map<String,String>> data = getContactInfo();
        System.out.println(data);
        lv_contact_number.setAdapter(new SimpleAdapter(this, data, R.layout.activity_contact__item_, new String[]{"name", "phone"}, new int[]{R.id.contact_item_name, R.id.contact_item_number}));
        lv_contact_number.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String number = data.get(position).get("phone").toString();
                System.out.println(number);
                Intent intent = new Intent();
                intent.putExtra("number", number);
                setResult(0,intent);
                finish();
            }
        });
    }
        private List<Map<String, String>> getContactInfo () {
            List<Map<String, String>> contactInfo = new ArrayList<Map<String, String>>();
            ContentResolver resolver = getContentResolver();
            Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
            Uri uriData = Uri.parse("content://com.android.contacts/data");
            Cursor cursor = resolver.query(uri, new String[]{"contact_id"}, null, null, null);
            while (cursor.moveToNext()) {
                String contact_id = cursor.getString(0);
                if (contact_id != null) {
                    Cursor dataCursor = resolver.query(uriData, new String[]{"data1", "mimetype"}, "contact_id=?", new String[]{contact_id}, null);
                    Map<String, String> map = new HashMap<String, String>();
                    while (dataCursor.moveToNext()) {
                        String data1 = dataCursor.getString(0);
                        String mimetype = dataCursor.getString(1);
                        if ("vnd.android.cursor.item/name".equals(mimetype)) {
                            map.put("name", data1);
                        }else if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
                            map.put("phone", data1);
                        }
                    }
                    contactInfo.add(map);
                    dataCursor.close();
                }

            }
            cursor.close();
            return contactInfo;
        }

    }
